	/*-------------*\
	|Crate Maker 2.0|
	|  By: find me  |
	\*-------------*/


TOOL.Category		= "#Construction"
TOOL.Name			= "#CrateMaker"
TOOL.Command		= nil
TOOL.ConfigName		= ""
TOOL.ClientConVar["PropHealth"] = "20"
TOOL.ClientConVar["mbox"] = "1"

Original_Object = {}
WaitTime = 0

if (CLIENT) then
	language.Add( "cratemaker", "Crate Maker" )
	language.Add( "Tool_cratemaker_name", "CrateMaker 2.0" )
	language.Add( "Tool_cratemaker_desc", "Turn things into crates!" )
	language.Add( "Tool_cratemaker_0", "Primary: Turn something into a crate.   Secondary: Mystery crate!   Reload: Break open crate." )
	language.Add("Undone_Crate", "Crate Undone")
	language.Add("Undone_Prop", "Prop Undone")
	language.Add("Undone_NPC", "NPC Undone")
	language.Add("Undone_Weapon", "Weapon Undone")
	language.Add("Undone_Entity", "Entity Undone")
end

local function IsReallyValid(tr)
	
	if (!tr.Entity) 			then return true end
	if (!tr.Entity:IsValid()) 	then return true end
	if (tr.Entity:IsPlayer())	then return true end
	if (tr.Entity:IsWorld()) 	then return true end
	if WaitTime > CurTime()		then return true end

	return false
end

local function ResetTable()
	local o = Original_Object
	o.object = nil
	o.skin = nil
	o.model = nil
	o.class = nil
	o.weapon = nil
	o.health = nil
	o.mbox = nil
end

function TOOL:LeftClick(tr)
if(CLIENT) then return true end
if WaitTime <= CurTime() then
	if IsReallyValid(tr) 					then return false end
	if (tr.Entity:GetClass() == "fm_cbox") 	then return false end
	if (tr.Entity:GetClass() == "fm_cmbox") then return false end
	local ent = tr.Entity
	local pos = ent:GetPos()
	local possibleweapon = ents.FindInSphere(pos, 2)
	Original_Object.health = self:GetClientNumber("PropHealth")
	Original_Object.mbox = self:GetClientNumber("mbox")
	
	if ent:GetClass() == "prop_physics" then
		Original_Object.object = 1 --prop
		Original_Object.skin = ent:GetSkin()
		Original_Object.model = ent:GetModel()
	elseif string.find(ent:GetClass(), "npc_") == 1 then
		Original_Object.object = 2 --npc
		Original_Object.skin = ent:GetSkin()
		Original_Object.model = ent:GetModel()
		Original_Object.class = ent:GetClass()
		for k, v in pairs(possibleweapon) do
			if string.find(v:GetClass(), "weapon_") == 1 then
				Original_Object.weapon = v:GetClass()
				--Original_Object.ammo = ent:GetAmmoCount()
			end
		end
	elseif string.find(ent:GetClass(), "weapon_") == 1 then
		Original_Object.object = 3 --weapon
		Original_Object.skin = ent:GetSkin()
		Original_Object.model = ent:GetModel()
		Original_Object.class = ent:GetClass()
	else -- I assume the only thing left could be an entity?
		Original_Object.object = 4 --entity
		Original_Object.skin = ent:GetSkin()
		Original_Object.model = ent:GetModel()
		Original_Object.class = ent:GetClass()
	end
	
	local Crate = ents.Create("fm_CBox")
	Crate:SetPos(pos + Vector(0, 0, 10))
	undo.Create("Crate")
		undo.AddEntity(Crate)
		undo.SetPlayer(self:GetOwner())
	undo.Finish()
	ent:Remove()
	Crate:Spawn()
	Crate:RecieveInfo(Original_Object)
	WaitTime = CurTime() + .5
	ResetTable()
end
end

function TOOL:RightClick(tr)
if(CLIENT) then return true end
	if WaitTime <= CurTime() then
		Original_Object.health = self:GetClientNumber("PropHealth")
		Original_Object.mbox = self:GetClientNumber("mbox")
		local R = math.random(1, 255)
		local G = math.random(1, 255)
		local B = math.random(1, 255)
		local A = math.random(100, 255)

		local Crate = ents.Create("fm_CMBox")
		Crate:SetPos(tr.HitPos)
		undo.Create("Crate")
			undo.AddEntity(Crate)
			undo.SetPlayer(self:GetOwner())
		undo.Finish()
		Crate:Spawn()
		Crate:RecieveInfo(Original_Object)
		if Original_Object.mbox == 1 then
			Crate:SetColor(R, G, B, 255)
		end
		WaitTime = CurTime() + .5
	end
end

function TOOL:Reload(tr)
if(CLIENT) then return true end
	if WaitTime <= CurTime() then
		if IsReallyValid(tr) then return false end -- Don't ask.
		if (tr.Entity:GetClass() == "fm_cbox") then
			local ent = tr.Entity

			ent:RemoveIt()
			Crate = nil
			WaitTime = CurTime() + .5
			ResetTable()
		elseif (tr.Entity:GetClass() == "fm_cmbox") then
			local ent = tr.Entity

			ent:RemoveIt()
			Crate = nil
			WaitTime = CurTime() + .5
			ResetTable()
		end
	end
end

function TOOL.BuildCPanel(CP)
	CP:AddControl("Header", { Text = "#Tool_cratemaker_name", Description = "#Tool_cratemaker_desc" })
	CP:AddControl("Slider",{Label = "Crate Health",	Type = "Int", Min = 1, Max = 500, Command = "cratemaker_PropHealth"})
	CP:AddControl("Checkbox",{Label = "Super mystery box", Command = "cratemaker_mbox"})
end